package com.blb.community.Utils;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.joda.time.DateTime;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * JWT工具类
 */
public class JwtUtils {

    public static final String JWT_KEY_USERNAME = "username";

    public static final int EXPIRE_MINUTES = 120;

    /**
     * 私钥加密token
     */
    public static String generateToken(String username, PrivateKey privateKey, int expireMinutes) {

        return Jwts.builder()
                .claim(JWT_KEY_USERNAME, username)
                .setExpiration(DateTime.now().plusMinutes(expireMinutes).toDate())
                .signWith(SignatureAlgorithm.RS256, privateKey)
                .compact();
    }

    /**
     * 从token解析用户
     *
     * @param token
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static String getUsernameFromToken(String token, PublicKey publicKey){
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(publicKey).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        String username = (String) body.get(JWT_KEY_USERNAME);
        return username;
    }

    public static void main(String[] args) {
        //生成jwt
    String token = JwtUtils.generateToken("刘宏彬", RsaUtils.privateKey, EXPIRE_MINUTES);
    System.out.println(token);
//            String token = "eyJhbGciOiJSUzI1NiJ9.eyJ1c2VybmFtZSI6InpoYW5nc2FuIiwiZXhwIjoxNjk5MzI4MDM4fQ.W8VZp4Yoqpu6nl_umosuY4ytO0IPRASV5NKWMDJh5np0wbqCW09h-HK-O_BcaNa42uhbv7ADU01uQVnKEZ7vWqF1M33bW92_eDK-fN4o_t_AJJsaWh6OeCsN5xoLRxt6SNGfdGGmZYRAy6fBx89C8w6r8UH4PLf7dhFV0ClJ-0w";
//            //解析jwt
//            String username = JwtUtils.getUsernameFromToken(token, RsaUtils.publicKey);
//            System.out.println(username);
    }
}
